package com.controller.manager;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.service.MaintenanceService;

@Controller
@RequestMapping("/maintenance")
public class MaintenanceController {

	@Resource
	private MaintenanceService maintenanceService;

	@RequestMapping(value = "/toMaintenancePage")
	public ModelAndView toMaintenancePage(HttpServletRequest request) {
		String username = (String) request.getSession().getAttribute("username");
		if (username == null || "".equals(username)) {
			return new ModelAndView("/login");
		}
		return new ModelAndView("/manager/maintenance");
	}

	@RequestMapping(value = "/addMaintenanceInfo", method = RequestMethod.POST)
	@ResponseBody
	public String addMaintenanceInfo(@RequestParam Map<String, String> paramsMap) {
		int result = maintenanceService.addMaintenanceInfo(paramsMap);
		if (result >= 0) {
			return "success";
		}
		return "error";
	}

	// 删除
	@RequestMapping(value = "/deleteMaintenanceInfo", method = RequestMethod.POST)
	@ResponseBody
	public String deleteMaintenanceInfo(@RequestParam Map<String, String> paramsMap) {
		try {
			int result = maintenanceService.deleteMaintenanceInfo(paramsMap);
			if (result > 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	@RequestMapping(value = "/updateMaintenanceInfo", method = RequestMethod.POST)
	@ResponseBody
	public String updateMaintenanceInfo(@RequestParam Map<String, String> paramsMap) {
		try {

			int result = maintenanceService.updateMaintenanceInfo(paramsMap);
			if (result > 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	@RequestMapping(value = "/getMaintenanceInfo", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, String>> getMaintenanceInfo(@RequestParam Map<String, String> paramsMap) {
		try {
			List<Map<String, String>> list = maintenanceService.getMaintenanceInfo(paramsMap);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@RequestMapping(value = "/getMaintenance", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView getMaintenance(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {
		List<Map<String, String>> list = maintenanceService.getMaintenanceByUuid(paramsMap);
		request.setAttribute("maintenance", list.get(0));
		return new ModelAndView("/manager/index_four");
	}
}
